<script setup lang="ts">
import { onMounted, onUnmounted, reactive, ref } from 'vue'
import { register, isRegistered, unregister, unregisterAll } from '@tauri-apps/api/globalShortcut'
import { listen } from '@tauri-apps/api/event'
import { LogicalPosition, LogicalSize, WebviewWindow, appWindow, getAll } from '@tauri-apps/api/window'
import SettingService from '@/services/setting.service'
import { invoke } from '@tauri-apps/api/tauri'

const domRef = ref<HTMLDivElement>()
const settingService = new SettingService()

onMounted(async () => {
  invoke('toggle_deltool')
  appWindow.setDecorations(false)
  const size = await appWindow.outerSize()
  size.width = 64
  size.height = 64
  await appWindow.setSize(size)
  const pos = await appWindow.outerPosition()
  pos.x = window.screen.availWidth - 15 - 64 - 120
  pos.y = 12
  await appWindow.setPosition(pos)
  await unregisterAll()
  // cross
  if (await isRegistered('CommandOrControl+Shift+C')) {
    await unregister('CommandOrControl+Shift+C')
  }
  const crossWin = new WebviewWindow('cross_win', {
    title: '',
    url: location.href + 'cross',
    width: 64,
    height: 64,
    maxWidth: 64,
    center: true,
    maximizable: false,
    minimizable: false,
    resizable: false,
    decorations: false,
    transparent: true,
    alwaysOnTop: true,
    visible: false,
    skipTaskbar: true
  })
  register('CommandOrControl+Shift+C', async () => {
    // crossConfig.value.opacity = +(!crossConfig.value.opacity)
    if (await crossWin.isVisible()) {
      crossWin.hide()
    } else{
      crossWin.show()
    }
  })
  // door
  WebviewWindow.getByLabel('door_win')?.close()
  const doorWin = new WebviewWindow('door_win', {
    title: '',
    url: location.href + 'door',
    width: 64,
    height: 64,
    fullscreen: true,
    center: true,
    resizable: false,
    decorations: false,
    transparent: true,
    alwaysOnTop: true,
    visible: false,
    skipTaskbar: true
  })
  if (await isRegistered('CommandOrControl+Shift+D')) {
    await unregister('CommandOrControl+Shift+D')
  }
  register('CommandOrControl+Shift+D', async () => {
    // logText.value = '计算中...'
    // resultValue.value = resultValue.value.map(() => false)
    // try {
    //   const results = await doorService.detect()
    //   const outputs = await doorService.openDoor(results)
    //   resultValue.value = outputs || resultValue.value
    //   if (resultValue.value.includes(true)) {
    //     logText.value = '计算完成'
    //     tempValues.splice(0, 19)
    //     autoClick()
    //   } else {
    //     logText.value = '计算失败'
    //     tempValues.splice(0, 19, ...results)
    //     appWindow.setIgnoreCursorEvents(false)
    //   }
    // } catch(e) {
    //   console.error(e)
    // }
    // setTimeout(() => {
    //   logText.value = ''
    // }, 4500)
    // setTimeout(() => {
    //   resultValue.value = resultValue.value.map(() => false)
    // }, 8000)
    console.log('try open door')
    doorWin.emit('open-door')
  })
  
  // map
  WebviewWindow.getByLabel('map')?.close()
  if (await isRegistered('CommandOrControl+Shift+M')) {
    await unregister('CommandOrControl+Shift+M')
  }
  const mapWin = new WebviewWindow('map', {
    url: location.href + 'map',
    width: window.screen.availWidth,
    height: window.screen.availHeight,
    center: true,
    resizable: false,
    decorations: false,
    skipTaskbar: true,
    alwaysOnTop: true,
    visible: false,
    transparent: true
  })
  register('CommandOrControl+Shift+M', async () => {
    if (await mapWin.isVisible()) {
      mapWin.hide()
    } else {
      const rect: number[] = await invoke('get_position')
      mapWin.setPosition(new LogicalPosition(rect[0], rect[1]))
      mapWin.setSize(new LogicalSize(rect[2], rect[3]))
      mapWin.show()
    }
  })
  
  // 挂机练体
  if (await isRegistered('CommandOrControl+Shift+H')) {
    await unregister('CommandOrControl+Shift+H')
  }
  register('CommandOrControl+Shift+H', async () => {
    invoke('toggle_hug')
  })
  // 发送当前设置
  invoke('update_hug_setting', {
    opts: {
      ...settingService.hugConfig,
      foods: settingService.hugConfig.foods.map(v => ({ ...v, key: +v.key })),
      shoe: {
        ...settingService.hugConfig.shoe,
        key: +settingService.hugConfig.shoe.key
      },
      glove: {
        ...settingService.hugConfig.glove,
        key: +settingService.hugConfig.glove.key
      },
      toolkits: settingService.hugConfig.toolkits.filter(v => v)
    }
  })
  // 主窗口
  const mainWindow = new WebviewWindow('main_win', {
    title: 'ASCUM',
    url: location.href + 'main',
    width: 640,
    height: 487,
    center: true,
    resizable: false,
    visible: false,
    decorations: false
  })
  listen('show-main', () => {
    mainWindow.show()
  })
  domRef.value?.addEventListener('dblclick', () => {
    mainWindow.show()
  })

  // press f shot
  // register('CommandOrControl+Shift+F', async () => {
  //   invoke('press_f_shot')
  // })

  setTimeout(() => appWindow.show(), 2000)
})
onUnmounted(async () => {
  await unregisterAll()
  getAll().filter(v => v.label !== 'main').forEach(v => {
    console.log(v.label)
    v.close()
  })
})
</script>

<template>
  <div class="home" data-tauri-drag-region @contextmenu.prevent ref="domRef">
    <div class="background-image"></div>
  </div>
</template>

<style scoped lang="less">
.home {
  width: 100vw;
  height: 100vh;
  box-sizing: content-box;
  display: flex;
  align-items: center;
  justify-content: center;
  background: transparent;
  pointer-events: all !important;
  cursor: move;
  position: relative;
  background-color: #00000066;

  .background-image {
    width: 80%;
    height: 80%;
    background-image: url(../../assets/128x128.png);
    background-size: 100%;
    opacity: 0.6;
    pointer-events: none;
  }
  &:hover .background-image {
    opacity: 0.85;
  }
}
</style>
<style>
body {
  background-color: transparent !important;
  background: transparent !important;
  pointer-events: none !important;
}
</style>
